package jiubeirobot.com.robotshop.ui.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import java.util.List;

import jiubeirobot.com.robotshop.R;
import jiubeirobot.com.robotshop.base.BaseRecyclerAdapter;
import jiubeirobot.com.robotshop.base.BaseViewHolder;
import jiubeirobot.com.robotshop.bean.ShopListBean;
import jiubeirobot.com.robotshop.bean.StockBean;
import jiubeirobot.com.robotshop.listener.ItemClick;
import jiubeirobot.com.robotshop.utils.L;
import jiubeirobot.com.robotshop.utils.TS;

/**
 * ==========================
 * 创建人：TheJoker丶豪 on
 * 创建时间：2017/8/22.
 * 创建内容：商品展示列表的适配器
 * ==========================
 */

public class ShopListAdapter extends BaseRecyclerAdapter<ShopListBean> {

    private CellClick cellClick;

    public void setCellClick(CellClick cellClick) {
        this.cellClick = cellClick;
    }

    public ShopListAdapter(Context context, List<ShopListBean> list) {
        super(context, list, R.layout.item_shop_list);
    }

    @Override
    public void convert(final BaseViewHolder holder, final ShopListBean shopListBean) {
        holder.setText(R.id.tv_shop_name, shopListBean.getGoods_name());
        holder.setText(R.id.tv_price, shopListBean.getGoods_current_price());
        holder.setText(R.id.tv_invitory,"库存："+shopListBean.getGoods_inventory());
        holder.setImg(R.id.iv_shop_img, shopListBean.getGoods_img());

        holder.getView(R.id.llyt_item_bg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (cellClick != null) {
                    cellClick.onItemClick(holder.getPostion());
                }
            }
        });


        holder.getView(R.id.tv_add_to_card).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cellClick != null) {
                    if (shopListBean.getGoods_inventory().equals("0")) {
                        TS.show("库存不足", 2);
                    } else {
                        int[] startLocation = new int[2];// 一个整型数组，用来存储按钮的在屏幕的X、Y坐标
                        v.getLocationInWindow(startLocation);// 这是获取购买按钮的在屏幕的X、Y坐标（这也是动画开始的坐标）
                        ImageView ball = new ImageView(context);
                        ball.setImageResource(R.mipmap.card_anim_icon);
                        L.i("startLocation：" + startLocation[0] + "    " + startLocation[1]);

                        //这是本地购物车的实体类  通过这个实体类来存储相应的参数
                        StockBean sbean = new StockBean();

                        sbean.setGoods_id(shopListBean.getGoods_id());
                        sbean.setGoods_img(shopListBean.getGoods_img());
                        sbean.setGoods_current_price(shopListBean.getGoods_current_price());
                        sbean.setGoods_inventory(shopListBean.getGoods_inventory());
                        sbean.setGoods_name(shopListBean.getGoods_name());
                        cellClick.addToCard(ball, startLocation, sbean, 1);
                    }
                }

            }

        });

    }

    public interface CellClick extends ItemClick {
        void addToCard(View view, int[] startLocation, StockBean stockBean, int shopNum);
    }
}
